<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%sys_address}}".
 *
 * @property integer $id
 * @property integer $userid
 * @property integer $type
 * @property integer $isdefault
 * @property string $province
 * @property string $city
 * @property string $area
 * @property string $town
 * @property string $address_info
 * @property string $address_detail
 * @property string $username
 * @property string $phone
 * @property string $tel
 * @property string $zipcode
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $status
 */
class Address extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%sys_address}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['userid', 'type', 'isdefault', 'create_time', 'update_time', 'status'], 'integer'],
            [['province', 'city', 'area', 'town'], 'string', 'max' => 40],
            [['address_info', 'address_detail'], 'string', 'max' => 200],
            [['username', 'phone', 'zipcode'], 'string', 'max' => 20],
            [['tel'], 'string', 'max' => 30]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'userid' => 'users表主键',
            'type' => '1收货，2发货',
            'isdefault' => '1常用，2非常用',
            'province' => '省',
            'city' => '市',
            'area' => '区',
            'town' => '街道',
            'address_info' => '省市区街道拼接串',
            'address_detail' => '详细地址',
            'username' => '联系人',
            'phone' => '手机号码',
            'tel' => '固定电话',
            'zipcode' => '邮编',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'status' => '状态：1可用，2禁用',
        ];
    }

    public function editAddress() {
        
    }

    public function addAddress($uid, $type, $isusual, $prov, $city, $area, $detail, $username, $phone, $zipcode, $tel, $addid) {
        //insert or update
        if (isset($addid) && $addid != "") {
            $address = Address::findOne($addid);
            //判断是否非法操作
            if ($address->userid != $uid) {
                return false;
            }
            $address->isdefault = $isusual;
            $address->province = $prov;
            $address->city = $city;
            $address->area = $area;
            $address->address_detail = $detail;
            $address->address_info = $prov . $city . $area;
            $address->username = $username;
            $address->phone = $phone;
            $address->update_time = time();
            $address->status = 1;
            $address->zipcode = $zipcode;
            $address->tel = $tel;
            if ($address->save()) {
                return $address->id;
            } else {
                return false;
            }
        } else {
            $num=self::find()->where("userid=$uid and type=1 and status=1")->count();
           
            if($num==0){
                $this->isdefault=1;
                
            }
    
            $this->userid = $uid;
            $this->type = $type;
           
            $this->province = $prov;
            $this->city = $city;
            $this->area = $area;
            $this->address_detail = $detail;
            $this->address_info = $prov . $city . $area;
            $this->username = $username;
            $this->phone = $phone;
            $this->create_time = time();
            $this->status = 1;
            $this->zipcode = $zipcode;
            $this->tel = $tel;
            if ($this->save()) {
                return $this->id;
            } else {
                return false;
            }
        }
    }
    
    /**
     * author：sunjian
     * time：2015年12月29日14:58:10
     * 获取用户收货地址
     * param: uid 用户id
     * **/
    public function getUserAddressList($uid){
       return $this->find()->where('userid='.$uid.' and type=1 and status=1')->asArray()->all();
    }
}
